package com.zhiqian.community.controller;

import com.zhiqian.community.entity.Page;
import com.zhiqian.community.service.DiscussPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class HomeController {
    @Autowired
    private DiscussPostService discussPostService;

    @GetMapping("/index")
    public String getIndexPage(Page page, Model model,@RequestParam(name = "orderMode",defaultValue = "0") int orderMode){
        //注意：在此方法调用前，SpringMVC会自动实例化Model和Page，并将Page注入到Model中，我们无需再注入，再Thymeleaf模板上直接用即可
        page.setRows(discussPostService.findDiscussPostRows(null));
        page.setPath("/index?orderMode="+orderMode);
        page.setLimit(15);
        List<Map<String, Object>> discussPosts = discussPostService.findDiscussPosts(null, page,orderMode);
        model.addAttribute("orderMode",orderMode);
        model.addAttribute("discussPosts",discussPosts);
        return "index";
    }

    @GetMapping("/error")
    public String getErrorPage(){
        return "/error/500";
    }
}
